﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcWeb.Models;
using System.Web.Security;
using System.Text;
using SailHero.Soft.FrameWork.IDomainRepositories.Contracts;
using SailHero.Soft.FrameWork.Models;
using UI.Web.SailHeroSoftAuthorization.Helper;

namespace MvcWeb.Controllers
{
    [Export(typeof(IController)), ExportMetadata("Name", "AuthHome")]
    public class AuthHomeController : BaseController
    {
        /// <summary>
        /// 数据操作接口
        /// </summary>
        [Import]
        public IModelRepository MModel;
        [Authorize]
        public ActionResult Index()
        {
            return View("index");
        }
        public ActionResult Center()
        {
            return View();
        }

        [Authorize]
        public ActionResult Top()
        {
            return View();
        }

        [Authorize]
        public ActionResult Left()
        {
            ViewData["UserPermission"] = GetPermisson();
            return View();
        }
        public ActionResult Down()
        {
            return View();
        }
        public ActionResult Right()
        {
            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        public void LogOut()
        {
            FormsAuthentication.SignOut();
            Response.Write("<script>top.location.href='/Login/Login';</script>");
        }

        protected string GetPermisson()
        {
            List<SYS_MODEL> modelList = MModel.GetListByUserPermission(LoginUser.USERID);//LoginUser.UserID
            PathHelper helper = new PathHelper();
            StringBuilder sb = new StringBuilder();
            if (modelList.Count > 0)
            {
                sb.Append("<ul style='height: 480px' id='navigation'>");
                List<SYS_MODEL> parentModList = (from m in modelList where (m.PARENTID == "" || m.PARENTID == null) orderby m.ORDERNUM select m).ToList();
                foreach (SYS_MODEL parentMod in parentModList)  //循环加载父模块
                {
                    sb.Append(" <li><a class='title'>" + parentMod.MODELCNAME + "</a><ul>");
                    List<SYS_MODEL> subModList = (from m in modelList where m.PARENTID == parentMod.MODELID orderby m.ORDERNUM select m).ToList();

                    foreach (SYS_MODEL subMod in subModList)  //循环加载子模块
                    {
                        sb.Append(" <li><a onclick='hc(this)' target='rightFrame' href='" + subMod.URL + "' title='" + subMod.MODELCNAME + "'>");
                        sb.Append("<img alt='' class='icon_space' border='0' src='" + helper.GetLocalPath() + "/Content/images/main/menu2.gif' />" + subMod.MODELCNAME + "</a></li>");
                    }

                    sb.Append("</ul></li>");
                }
                sb.Append("</ul>");
            }
            return sb.ToString();
        }
    }
}
